const ejs = require('ejs')

let { parsed, finalTemplate, content } = require('../yaml-front-matter')()
const replaceBlockRE = /<%# REPLACE %>([^]*?)<%# END_REPLACE %>/g

// console.log(parsed)

// console.log('start:', finalTemplate)

finalTemplate = `<%_ if (${parsed.when}) { _%>` + finalTemplate + `<%_ } _%>`

// console.log('ending:', finalTemplate)

// console.log('replaceBlockRE', content.match(replaceBlockRE))

// 依据 index.vue 文件，准备的模拟数据
let data = {
  rootOptions: { plugins: { '@vue/cli-plugin-router': 'x.x.x' } },
  // rootOptions: { plugins: {} },
  options: {
    classComponent: 'typescript config experimentalDecorators || isTypescript'
  }
}

const result = ejs.render(finalTemplate, data, {})

const replaceMatch = content.match(replaceBlockRE)
console.log('replaceMatch', replaceMatch)
const replaces = replaceMatch.map(m => {
  return m.replace(replaceBlockRE, '$1').trim()
})

console.log('replaces', replaces)

parsed.replace.forEach((r, i) => {
  finalTemplate = finalTemplate.replace(r, replaces[i])
})

console.log(result)
